package Socket;

// TCPClientGUI.java
// TCP Client that reads and displays information sent from a Server.  GUI Version
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class TCPClient {
	private static ObjectOutputStream output; // output stream to server
	private static ObjectInputStream input; // input stream from server
	private static String message = ""; // message from server
	private final static String chatServer = "10.32.122.217"; // host server for
																// this
																// application
	private static Socket client; // socket to communicate with server
	// connect to server and process messages from server
	public static void runClient() throws EOFException, IOException {

		connectToServer(); // create a Socket to make connection
		getStreams(); // get the input and output streams
		processConnection(); // process connection
		closeConnection(); // close connection

	} // end method runClient

	// connect to server
	public static void connectToServer() throws IOException {
		System.out.println("Attempting connection\n");

		// create Socket to make connection to server
		client = new Socket(InetAddress.getByName(chatServer), 5430);

		// display connection information
		System.out.println("Connected to: "
				+ client.getInetAddress().getHostName());
	} // end method connectToServer

	// get streams to send and receive data
	public static void getStreams() throws IOException {
		// set up output stream for objects
		output = new ObjectOutputStream(client.getOutputStream());
		output.flush(); // flush output buffer to send header information

		// set up input stream for objects
		input = new ObjectInputStream(client.getInputStream());

		System.out.println("\nGot I/O streams\n");
	} // end method getStreams

	// process connection with server
	public static void processConnection() throws IOException {
		do // process messages sent from server
		{
			try // read message and display it
			{
				message = (String) input.readObject(); // read new message
				System.out.println("\n" + message); // display message
			} // end try
			catch (ClassNotFoundException classNotFoundException) {
				System.out.println("\nUnknown object type received");
			} // end catch

		} while (!message.equals("SERVER>>> TERMINATE"));
	} // end method processConnection

	// close streams and socket
	public static void closeConnection() {
		System.out.println("\nClosing connection");
		try {
			output.close(); // close output stream
			input.close(); // close input stream
			client.close(); // close socket
		} // end try
		catch (IOException ioException) {
			ioException.printStackTrace();
		} // end catch
	} // end method closeConnection

	// send message to server
	public static void sendData(String message) {
		try // send object to server
		{
			output.writeObject("CLIENT>>> " + message);
			output.flush(); // flush data to output
			System.out.println("\nCLIENT>>> " + message);
		} // end try
		catch (IOException ioException) {
			System.out.println("\nError writing object");
		} // end catch
		catch (NullPointerException nullPointerException) {
			System.out.println("\nError writing object");
		}
	} // end method sendData
	
	// send message to server
	public static void sendData(byte[] message) {
		sendData(new String(message));

	} // end method sendData

} // end class TCPClientGUI
